package com.zks.example.bingfa;

import java.util.concurrent.*;

public class MyThreadPoolExecutor extends ThreadPoolExecutor {

    public MyThreadPoolExecutor(int corePoolSize, int maxiumPoolSize,
                                long keepAliveTime, TimeUnit timeUnit, BlockingQueue<Runnable> workQuery){
        super(corePoolSize,maxiumPoolSize,keepAliveTime,timeUnit,workQuery);
    }

    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(wrap(task,clientTrace(),Thread.currentThread().getName()));
    }

    @Override
    public void execute(Runnable command) {
        super.execute(wrap(command,clientTrace(),Thread.currentThread().getName()));
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return super.submit(task);
    }

    private Exception clientTrace() {
        return  new Exception("client stack trace");
    }

    private Runnable wrap(final Runnable task,final Exception clientStack,String  clientThreadName){
        return new Runnable() {
            @Override
            public void run() {
                try {
                    task.run();
                }catch (Exception e){
                    clientStack.printStackTrace();
                    throw e;
                }
            }
        };

    }

    public static void main(String[] args) {
       ThreadPoolExecutor poolExecutor=new MyThreadPoolExecutor(0,Integer.MAX_VALUE,0l,
                TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
        ExecutorService executorService =Executors.newCachedThreadPool();

        for(int i=0;i<5;i++){
            poolExecutor.execute(new DivTask(100,i));
            //executorService.execute(new DivTask(100,i));
        }
    }
}

